/*
 *      Comparision.java
 *
 *      Copyright 2008 Pablo Nicolas Diaz Bilotto <pablonicolas.diaz@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
package ilanguage.iltokens.operations.logical;

import i386CodeGenerator.RegisterAssigner;
import ilanguage.iltokens.IL_SSA;
import ilanguage.iltokens.operations.TypeSafeTwoOperandIL_SSA;
import lexer.EntryType;

/**
 * The Class Comparision.
 */
public abstract class Comparision extends TypeSafeTwoOperandIL_SSA {

	/**
	 * Instantiates a new comparision.
	 *
	 * @param firstToken the first token
	 * @param secToken the sec token
	 */
	public Comparision(IL_SSA firstToken, IL_SSA secToken) {
		super(firstToken, secToken);
	}

	@Override
	public String getOperationName() {
		return null;
	}

	@Override
	public String GenerateCode() 
	{
		IL_SSA firstArg = this.getFirstArg();
		IL_SSA secondArg = this.getSecondArg();
		String codigo=  "\t; Comienzo de comparacion\n";  

	    if (this.solveTypes() == EntryType.INT)
	    {
	    	codigo+=compInt(firstArg.getResultRegister(),secondArg.getResultRegister());
	 	}
	    else
	    {
	    	codigo+=compDouble(firstArg,secondArg);
		}
		 
	     codigo+=  "\t; Fin de comparacion\n\n";
	     this.setResultRegister(this.getOperationName());
	     return codigo;
	}

	private String compDouble(IL_SSA firstArg, IL_SSA secondArg) {
		String codigo="";
		String first = firstArg.getResultRegister();
		String second  = secondArg.getResultRegister();
		
		if (first !="FPU"){
			if (!first.startsWith("_")){
				codigo+="\t\tmov varauxDouble,"+ firstArg.getResultRegister()+"\n";
				codigo+="\t\tfld varauxDouble\n";
			}
			else
				codigo+="\t\tfld "+ firstArg.getResultRegister()+"\n";
		}
		if (secondArg.getResultRegister()!="FPU"){
			if (!second.startsWith("_")){
				codigo+="\t\tmov varauxDouble,"+ secondArg.getValue().toString()+"\n";
				codigo+="\t\tfld varauxDouble\n";
			}
			else
				codigo+="\t\tfld "+ secondArg.getResultRegister()+"\n";
		}
		
		codigo+="\t\tfcompp\n";
		codigo+="\t\tfstsw FlagVar\n";
		codigo+="\t\tmov ax, FlagVar\n";
		codigo+="\t\tsahf \n";
		return codigo;
	}

	private String compInt(String firstArg, String secondArg) 
	{
		String codigo="";
		 	
		if (RegisterAssigner.getInstance().esRegistro(firstArg)){
	   		 codigo+="\t\tcmp "+firstArg+ " , "+ secondArg+"\n";
	   		 RegisterAssigner.getInstance().liberarRegistro(firstArg);
	   		 RegisterAssigner.getInstance().liberarRegistro(secondArg);
	   		 this.setResultRegister(this.getOperationName());
			}
	   	 else { //si ninguno de los dos es registro
	   		 	codigo+="\t\tmov ax"+ ","+ firstArg+"\n";
	   		 	codigo+="\t\tcmp ax"+ ","+ secondArg+"\n";
	   		 	this.setResultRegister(this.getOperationName());
	   		    RegisterAssigner.getInstance().liberarRegistro(firstArg);
	   		    RegisterAssigner.getInstance().liberarRegistro(secondArg);
	   	 	  }
		
		return codigo;	
	}
}